import { _decorator, Component, Node, EventTouch, UITransformComponent, Vec3, macro, UIOpacityComponent, CCInteger } from 'cc';
const { ccclass, property } = _decorator;
import { Players } from '../node/Players';

@ccclass('Rocker')
export class Rocker extends Component {
    @property({ type: Node, tooltip: '移动中心节点' })
    stick: Node = null;

    @property({ type: Node, tooltip: '摇杆背景节点' })
    joyBk: Node = null;

    @property(Node)
    PlayerManager: Node = null;

    private _playerController = null;
    private _uITransform: UITransformComponent = null;
    private _nodeParent: Node = null;
    private _maxR: number = 100;

    onLoad() {
        // 归位
        this._goBackMid();
        for (let index = 0; index < this.PlayerManager.children.length; index++) {
            if (/^Player/.test(this.PlayerManager.children[index].name)) {
                this._playerController = this.PlayerManager.children[index].getComponent(Players);
                break;
            };
        };
    }

    start() {
        this._nodeParent = this.node.parent;
        this._uITransform = this.getComponent(UITransformComponent);

        // 监听全局，摇杆可以在任意位置出现
        this._nodeParent.on(Node.EventType.TOUCH_START, this._onTouchStart, this);
        this._nodeParent.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this._nodeParent.on(Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this._nodeParent.on(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
    }

    /** 回归中心 */
    private _goBackMid(): void {
        this.getComponent(UIOpacityComponent).opacity = 0;
        this.stick.setPosition(0, 0, 0);
    }

    private _onTouchStart(e: EventTouch): void {
        this._onTouchMove(e);
        let localPosition: Vec3 = new Vec3();
        this.getComponent(UIOpacityComponent).opacity = 255;
        let UITC: UITransformComponent = this._nodeParent.getComponent(UITransformComponent);
        UITC.convertToNodeSpaceAR(new Vec3(e.getUILocation().x, e.getUILocation().y, 0), localPosition);
        this.stick.setPosition(0, 0, 0);
        this.node.setPosition(localPosition);
    }

    private _onTouchMove(e: EventTouch): void {
        const location = e.getUILocation();
        // 坐标转换
        let pos = this._uITransform.convertToNodeSpaceAR(new Vec3(location.x, location.y));
        // 根据半径限制位置
        this._clampPos(pos);
        // 设置中间点的位置
        this.stick.setPosition(pos.x, pos.y, 0);
        // 算出与(1,0)的夹角
        let angle = this._covertToAngle(pos);
        // 触发回调
        this._playerController.joysitckCallback(pos, angle);
    }

    private _onTouchEnd(e: EventTouch): void {
        this._goBackMid();
        this._playerController.joysitckCallback(new Vec3(0, 0, 0), 0);
    }

    /** 根据半径限制位置 */
    private _clampPos(pos: Vec3): void {
        let len = pos.length();
        if (len > this._maxR) {
            let k = this._maxR / len;
            pos.x *= k;
            pos.y *= k;
        }
    }

    /** 根据位置转化角度 */
    private _covertToAngle(pos: Vec3): number {
        let angle = Math.atan2(pos.y, pos.x);
        return angle * macro.DEG;
    }

    // 关闭监听
    onDisable() {
        this._goBackMid();
        this._nodeParent.off(Node.EventType.TOUCH_START, this._onTouchStart, this);
        this._nodeParent.off(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this._nodeParent.off(Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this._nodeParent.off(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
    }
}
